package com.cz.mydao.config;

import com.cz.mydao.enumeration.DatabaseType;
import com.cz.mydao.constant.Constant;
import com.cz.mydao.pool.CustomConnectionPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;

/**
 * @descriptions: 连接池配置类
 * @author: Joker.
 * @date: 2024/12/23 10:55
 * @version: 1.0
 */
@Configuration
public class DataSourceConfig {

    /**
     * 枚举类型  数据库类型
     */
    @Value("${spring.datasource.data-type}")
    private DatabaseType databaseType;

    /**
     * 生成mysql的连接池
     */
    @Resource
    private MySQLConnectionPool mysqlDataSourceProvider;

    /**
     * 生成sqlserver的连接池
     */
    @Resource
    private SQLServerConnectionPool sqlServerDataSourceProvider;

    /**
     * 根据配置文件返回对应的数据库连接池
     * @return 数据库连接池
     */
    @Bean
    public CustomConnectionPool getDataSource() {
        switch (databaseType) {
            case MYSQL:
                return mysqlDataSourceProvider.createConnectionPool();
            case SQL_SERVER:
                return sqlServerDataSourceProvider.createConnectionPool();
            default:
                throw new RuntimeException(Constant.NOT_FOUND_DB_TYPE);
        }
    }

}
